#!/bin/bash
#Author haipeng
#date:2022.03.28
# Download
# ./install.sh download
# Install Nginx,default install dir: ./nginx
# ./install
# versions
PCRE_VERISON=8.45
ZLIB_VERSION=1.2.12
OPEN_SSL_VERSION=1.1.1n
NGINX_VERSION=1.20.2

# work dir
ROOT=$(cd $(dirname '$0');pwd)
cd $ROOT
mkdir nginx-download
mkdir nginx  # nginx default install dir
mkdir openssl
mkdir pcre
mkdir zlib
INSTALL_PATH=$ROOT/nginx

if [ "$1" = "download" ] ;then
    # downlaod pcre
    cd $ROOT/pcre
    #wget https://ftp.pcre.org/pub/pcre/pcre-$PCRE_VERISON.tar.gz .
    wget https://nchc.dl.sourceforge.net/project/pcre/pcre/$PCRE_VERISON/pcre-$PCRE_VERISON.tar.gz

    # downlaod zlib
    cd $ROOT/zlib
    wget http://zlib.net/zlib-$ZLIB_VERSION.tar.gz

    # download openssl
    cd $ROOT/openssl
    wget http://www.openssl.org/source/openssl-$OPEN_SSL_VERSION.tar.gz .

    # download nginx
    cd $ROOT/nginx-download
    wget https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz .
else
    # install pcre
    cd $ROOT/pcre
    tar -zxvf pcre-$PCRE_VERISON.tar.gz
    ./configure
    make
    make install

    # install zlib
    cd $ROOT/zlib
    tar -zxvf zlib-$ZLIB_VERSION.tar.gz
    ./configure
    make
    make install

    # insall openssl
    cd $ROOT/openssl
    tar -zxvf openssl-$OPEN_SSL_VERSION.tar.gz

    # install nginx
    cd $ROOT/nginx-download
    tar -zxvf nginx-$NGINX_VERSION.tar.gz
    cd nginx-$NGINX_VERSION
    ./configure --sbin-path=$INSTALL_PATH \
    --prefix=$INSTALL_PATH \
    --conf-path=$INSTALL_PATH/conf/nginx.conf \
    --pid-path=$INSTALL_PATH/nginx.pid \
    --with-http_ssl_module \
    --with-stream \
    --with-pcre=$ROOT/pcre/pcre-$PCRE_VERISON \
    --with-zlib=$ROOT/zlib/zlib-$ZLIB_VERSION \
    --with-openssl=$ROOT/openssl/openssl-$OPEN_SSL_VERSION
make
make install
fi
exit 0